document.addEventListener('DOMContentLoaded', async function () {




    /////////////获取统计数据并渲染//////////////
    const res = await axios({
        url: '/dashboard',
        method: 'get',
        // //在请求头中携带token
        // headers : {
        //     authorization : localStorage.getItem('user-token')
        // }
    })
    console.log('服务器响应回来的数据', res);
    const overview = res.data.overview
    console.log('橄榄数据', overview);


    const spans = [...document.querySelectorAll('span[name]')]
    spans.forEach(item => {
        console.log(item);
        const k = item.getAttribute('name')
        item.innerHTML = overview[k]
    })

    //全年学科薪资走势图（折线图）
    renderSalary(res.data.year)

    //班级薪资分布图（饼图）
    renderClass(res.data.salaryData)

    //班级小组薪资情况图（柱状图）
    renderGroup(res.data.groupData)

    //男女薪资分布图表（两个饼图）
    renderGender(res.data.salaryData) 

    initMapChart(res.data.provinceData)
})

const initMapChart = (provinceData) => {
    const myEchart = echarts.init(document.querySelector('#map'))
    const dataList = [
      { name: '南海诸岛', value: 0 },
      { name: '北京', value: 0 },
      { name: '天津', value: 0 },
      { name: '上海', value: 0 },
      { name: '重庆', value: 0 },
      { name: '河北', value: 0 },
      { name: '河南', value: 0 },
      { name: '云南', value: 0 },
      { name: '辽宁', value: 0 },
      { name: '黑龙江', value: 0 },
      { name: '湖南', value: 0 },
      { name: '安徽', value: 0 },
      { name: '山东', value: 0 },
      { name: '新疆', value: 0 },
      { name: '江苏', value: 0 },
      { name: '浙江', value: 0 },
      { name: '江西', value: 0 },
      { name: '湖北', value: 0 },
      { name: '广西', value: 0 },
      { name: '甘肃', value: 0 },
      { name: '山西', value: 0 },
      { name: '内蒙古', value: 0 },
      { name: '陕西', value: 0 },
      { name: '吉林', value: 0 },
      { name: '福建', value: 0 },
      { name: '贵州', value: 0 },
      { name: '广东', value: 0 },
      { name: '青海', value: 0 },
      { name: '西藏', value: 0 },
      { name: '四川', value: 0 },
      { name: '宁夏', value: 0 },
      { name: '海南', value: 0 },
      { name: '台湾', value: 0 },
      { name: '香港', value: 0 },
      { name: '澳门', value: 0 },
    ]

    dataList.forEach(item => {
        const obj = provinceData.find(it => {
            return it.name.includes(item.name)
        })
        if(obj) {
            item.value = obj.value
        }
    })

    let option = {
      title: {
        text: '籍贯分布',
        top: 10,
        left: 10,
        textStyle: {
          fontSize: 16,
        },
      },
      tooltip: {
        trigger: 'item',
        formatter: '{b}: {c} 位学员',
        borderColor: 'transparent',
        backgroundColor: 'rgba(0,0,0,0.5)',
        textStyle: {
          color: '#fff',
        },
      },
      visualMap: {
        min: 0,
        max: 6,
        left: 'left',
        bottom: '20',
        text: ['6', '0'],
        inRange: {
          color: ['#ffffff', '#0075F0'],
        },
        show: true,
        left: 40,
      },
      geo: {
        map: 'china',
        roam: false,
        zoom: 1.0,
        label: {
          normal: {
            show: true,
            fontSize: '10',
            color: 'rgba(0,0,0,0.7)',
          },
        },
        itemStyle: {
          normal: {
            borderColor: 'rgba(0, 0, 0, 0.2)',
            color: '#e0ffff',
          },
          emphasis: {
            areaColor: '#34D39A',
            shadowOffsetX: 0,
            shadowOffsetY: 0,
            shadowBlur: 20,
            borderWidth: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)',
          },
        },
      },
      series: [
        {
          name: '籍贯分布',
          type: 'map',
          geoIndex: 0,
          data: dataList,
        },
      ],
    }
    myEchart.setOption(option)
  }

function renderGender(salaryData) {
    const myChart = echarts.init(document.querySelector('#gender'))
    const option = {
        title: [
          {
            text: '男女薪资分布',
            left: 10,
            top: 15,
            textStyle: {
              fontSize: 16
            }
          },
          {
            text: '男生',
            left: '48%',
            top: '48%',
            textStyle: {
              fontSize: 14
            }
          },
          {
            text: '女生',
            left: '48%',
            top: '89%',
            textStyle: {
              fontSize: 14
            }
          },
        ],
        tooltip: {
          trigger: 'item'
        },
        color: ['#fda224', '#5097ff', '#3abcfa', '#34d39a'],
        series: [
          {
            name: '男生薪资',
            type: 'pie',
            radius: ['20%', '30%'],
            center: ['50%', '30%'],
            // data: [
            //   { value: 1048, name: 'Search Engine' },
            //   { value: 735, name: 'Direct' },
            //   { value: 580, name: 'Email' },
            //   { value: 484, name: 'Union Ads' }
            // ]
            data: salaryData.map(item => {
                return {value: item.b_count, name: item.label}
            })
          },
          {
            name: '女生薪资',
            type: 'pie',
            radius: ['20%', '30%'],
            center: ['50%', '70%'],
            // data: [
            //   { value: 1048, name: 'Search Engine' },
            //   { value: 735, name: 'Direct' },
            //   { value: 580, name: 'Email' },
            //   { value: 484, name: 'Union Ads' }
            // ]
            data: salaryData.map(item => {
                return {value: item.g_count, name: item.label}
            })
          }
        ]
      };

    myChart.setOption(option)
}

function renderGroup(groupData) {
    const myChart = echarts.init(document.querySelector('#lines'))
    const option = {
        tooltip: {
            trigger: 'item'
        },
        // grid网格
        grid: {
            left: 30,
            top: 30,
            right: 30,
            bottom: 50
        },
        xAxis: {
            type: 'category',
            //data: ['1月', '2月', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
            data: groupData[1].map(item => item.name),
            // 轴线
            axisLine: {
                lineStyle: {
                    type: 'dashed',
                    color: '#ccc'
                }
            },
            // 刻度标签
            axisLabel: {
                color: '#999'
            }
        },
        yAxis: {
            type: 'value',
            // 分隔线的数量
            splitNumber: 5,
            // 分隔线
            splitLine: {
                lineStyle: {
                    type: 'dashed',
                    color: 'pink'
                }
            }
        },
        color: [
            {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                    {
                        offset: 0,
                        color: 'rgba(63, 213, 159,1)' // 0% 处的颜色
                    },
                    {
                        offset: 1,
                        color: 'rgba(63, 213, 159,0.1)' // 100% 处的颜色
                    }
                ],
                global: false // 缺省为 false
            },
            {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                    {
                        offset: 0,
                        color: 'rgba(78, 161, 238,1)' // 0% 处的颜色
                    },
                    {
                        offset: 1,
                        color: 'rgba(78, 161, 238,0.1)' // 100% 处的颜色
                    }
                ],
                global: false // 缺省为 false
            }
        ],
        series: [
            {
                // data: [120, 200, 150, 80, 70, 110, 130],
                data: groupData[1].map(item => item.hope_salary),
                type: 'bar',
                name: '期望薪资'
            },
            {
                //data: [120, 200, 150, 80, 70, 110, 130],
                data: groupData[1].map(item => item.salary),
                type: 'bar',
                name: '实际薪资'
            }
        ]
    };

    myChart.setOption(option)

    //给按钮注册点击事件
    const btns = document.querySelector('#btns')
    btns.addEventListener('click', function (e) {
        if (e.target.tagName === 'BUTTON') {
            const n = e.target.innerHTML
            // console.log(groupData[n]);

            option.xAxis.data = groupData[n].map(item => item.name)
            option.series[0].data = groupData[n].map(item => item.hope_salary)
            option.series[1].data = groupData[n].map(item => item.salary)

            myChart.setOption(option)

            btns.querySelector('.btn-blue').classList.remove('btn-blue')
            e.target.classList.add('btn-blue')
        }
    })
}

function renderClass(salaryData) {
    const myChart = echarts.init(document.querySelector('#salary'))
    const option = {
        title: {
            text: '班级薪资分布',
            textStyle: {
                fontSize: 16
            },
            left: 10,
            top: 15
        },
        color: ['#fda224', '#5097ff', '#3abcfa', '#34d39a'],
        tooltip: {
            trigger: 'item'
        },
        legend: {
            bottom: '5%',
            left: 'center'
        },
        series: [
            {
                name: 'Access From',
                type: 'pie',
                //圆的半径
                radius: ['50%', '65%'],
                //圆心
                center: ['50%', '45%'],
                avoidLabelOverlap: false,
                itemStyle: {
                    borderRadius: 5,
                    borderColor: '#fff',
                    borderWidth: 2
                },
                label: {
                    show: false,
                    position: 'center'
                },
                //高亮样式
                // emphasis: {
                //   label: {
                //     show: true,
                //     fontSize: 40,
                //     fontWeight: 'bold'
                //   }
                // },
                labelLine: {
                    show: false
                },
                // data: [
                //   { value: 1048, name: 'Search Engine' },
                //   { value: 735, name: 'Direct' },
                //   { value: 580, name: 'Email' },
                //   { value: 484, name: 'Union Ads' },
                // ]

                data: salaryData.map(item => {
                    return {
                        name: item.label,
                        value: item.g_count + item.b_count
                    }
                })
            }
        ]
    };

    myChart.setOption(option)
}

function renderSalary(year) {
    const myChart = echarts.init(document.querySelector('#line'))
    const option = {
        //标题
        title: {
            text: '2021年全学科薪资走向',
            textStyle: {
                fontSize: 16
            },
            left: 10,
            top: 15,
        },
        xAxis: {
            type: 'category',
            //data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
            data: year.map(item => item.month),
            //轴线
            axisLine: {
                lineStyle: {
                    type: 'dashed',
                    color: '#ccc',
                },
            },
            //刻度标签
            axisLabel: {
                color: '#999'
            }
        },
        yAxis: {
            type: 'value',
            //分割线的数量
            splitNumber: 5,
            //分割线
            splitLine: {
                lineStyle: {
                    type: 'dashed',
                    color: 'pink',
                }
            }
        },
        //提示框
        tooltip: {
            trigger: 'axis',
        },
        //网络
        grid: {
            left: '10%',
            top: '20%'
        },
        color: [
            {
                type: 'linear',
                // 两个点的坐标（坐标的原点是左上角）---渐变方向
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                // 渐变颜色
                colorStops: [{
                    offset: 0,
                    color: '#4a9dee' // 0% 处的颜色
                },
                {
                    offset: 1,
                    color: '#5b78ef' // 100% 处的颜色
                }
                ],
                global: false // 缺省为 false
            }
        ],
        series: [
            {
                // data: [820, 932, 901, 934, 1290, 1330, 1320],
                data: year.map(item => item.salary),
                type: 'line',
                //是否平滑显示
                smooth: true,
                //线的样式
                lineStyle: {
                    width: 4
                },
                //标记的大小
                symbolSize: 10,
                // 区域填充
                areaStyle: {
                    color: {
                        type: 'linear',
                        // 两个点的坐标（坐标的原点是左上角）---渐变方向
                        x: 0,
                        y: 0,
                        x2: 0,
                        y2: 1,
                        // 渐变颜色
                        colorStops: [
                            {
                                offset: 0,
                                color: 'rgba(73, 156, 238, 0.8)' // 0% 处的颜色
                            },
                            {
                                offset: 0.6,
                                color: 'rgba(73, 156, 238, 0.1)' // 80% 处的颜色
                            },
                            {
                                offset: 1,
                                color: 'rgba(255,255,255,0)' // 100% 处的颜色
                            }
                        ],
                        global: false // 缺省为 false
                    }
                }
            }
        ]
    };
    myChart.setOption(option)
}


